package com.lishe.lishe.lishe.module.excitation.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.bean.SpaceItemDecoration;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.excitation.adapter.PersonReceiveAdmireAdapter;
import com.lishe.lishe.lishe.module.excitation.bean.admire.AdmireObject;
import com.lishe.lishe.lishe.module.excitation.presenter.ExcitationPresenter;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.retrofit.DataResponseListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang on 2017/11/28.
 * 收到鼓励列表
 */

public class PersonReceiveAdmireActivity extends BaseActivity implements View.OnClickListener {

    public static void jump(Activity activity, String eventId) {
        Intent intent = new Intent(activity, PersonReceiveAdmireActivity.class);
        intent.putExtra("eventId", eventId);
        activity.startActivity(intent);
    }

    private TextView mTextTitle, mTextEmpty;
    private Button mButtonBack;
    private RecyclerView mRecyclerList;
    private RelativeLayout mRelativeEmpty, mRelativeError;

    private List<AdmireObject> mList;
    private String mEventId;
    private PersonReceiveAdmireAdapter mAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_receive_admire);
        mEventId = getIntent().getStringExtra("eventId");
        initView();
        setListener();
        setAdapter();
        getAdmireList();
    }

    private void initView() {
        mTextTitle = findViewById(R.id.title_text_title);
        mButtonBack = findViewById(R.id.title_left_button);
        mRecyclerList = findViewById(R.id.list);
        mRelativeEmpty = findViewById(R.id.empty);
        mRelativeError = findViewById(R.id.error);
        mTextEmpty = findViewById(R.id.empty_text);

        mTextTitle.setText("收到鼓励列表");
        mTextEmpty.setText("数据为空！");
    }

    private void setListener() {
        mButtonBack.setOnClickListener(this);
        mRelativeError.setOnClickListener(this);
    }

    private void setAdapter() {
        mList = new ArrayList<>();
        mAdapter = new PersonReceiveAdmireAdapter(this, mList);
        mRecyclerList.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerList.addItemDecoration(new SpaceItemDecoration(0, 0, 0, Utils.dip2px(this, 0.5f)));
        mRecyclerList.setAdapter(mAdapter);
    }

    private void getAdmireList() {
        if (mRelativeEmpty.getVisibility() == View.VISIBLE) {
            mRelativeEmpty.setVisibility(View.GONE);
        }
        if (mRelativeError.getVisibility() == View.VISIBLE) {
            mRelativeError.setVisibility(View.GONE);
        }

        ExcitationPresenter.getInstance().getAdmireList(Globals.getToken(), mEventId, new DataResponseListener<List<AdmireObject>>() {

            @Override
            public void onPrepare() {
                super.onPrepare();
                LoadingUtils.createLoadingDialog(PersonReceiveAdmireActivity.this, "请稍候...", false);
            }

            @Override
            public void onDataEmpty() {
                super.onDataEmpty();
                mRelativeEmpty.setVisibility(View.VISIBLE);
            }

            @Override
            public void onSuccess(List<AdmireObject> dataResponse) {
                LoadingUtils.closeDialog();
                mList.clear();
                mList.addAll(dataResponse);
                mAdapter.notifyDataSetChanged();

                if (dataResponse.size() == 0) {
                    mRelativeEmpty.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFail(String failMessage) {
                toast(failMessage);
                mRelativeEmpty.setVisibility(View.VISIBLE);
                LoadingUtils.closeDialog();
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                mRelativeError.setVisibility(View.VISIBLE);
                LoadingUtils.closeDialog();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_button:
                finish();
                break;
        }
    }
}
